package com.bookdresser.widget;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import com.bookdresser.R;

public class TextEdiText extends LinearLayout implements EdtInterface {

	private ImageButton cancel_ib;
	private EditText et;

	public TextEdiText(Context context) {
		super(context);
	}

	public TextEdiText(Context context, AttributeSet attrs) {
		super(context);
		LayoutInflater.from(context).inflate(R.layout.edit_text_layout, this,
				true);
		init();
	}

	private void init() {
		cancel_ib = (ImageButton) findViewById(R.id.cancel_ib);
		et = (EditText) findViewById(R.id.et);
		et.addTextChangedListener(tw);
		cancel_ib.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				hideBtn();
				et.setText("");
			}
		});
	}

	TextWatcher tw = new TextWatcher() {
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
		}

		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
		}

		public void afterTextChanged(Editable s) {
			if (s.length() == 0) {
				hideBtn();
			} else {
				showBtn();
			}
		}
	};

	public void hideBtn() {
		if (cancel_ib.isShown())
			cancel_ib.setVisibility(View.GONE);
	}

	public void showBtn() {
		if (!cancel_ib.isShown())
			cancel_ib.setVisibility(View.VISIBLE);
	}
}